/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Samuel.OfficeAssistant.GUI.initComponent;

import Samuel.Util.PathLoad;
import com.sun.awt.AWTUtilities;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

/**
 *
 * @author samuel
 */
public class InitFrame {

    public InitFrame() {
        init();
    }
    private JFrame frame = new JFrame();
    /**
     * 获取需要的资源文件
     */
    private ImageIcon topLeftPicPath = PathLoad.getImageIcon("top_left.png");
    private ImageIcon topMidPicPath = PathLoad.getImageIcon("top_middle.png");
    private ImageIcon rightPicPath = PathLoad.getImageIcon("mid_Color2.png");
    
    private ImageIcon CloseBtnUnselectPath = PathLoad.getImageIcon("top_right_unselect.png");
    private ImageIcon CloseBtnSelectedPath = PathLoad.getImageIcon("top_right_selected.png");
    private JButton btnClose = createTransparentButton(CloseBtnUnselectPath, CloseBtnSelectedPath, CloseBtnUnselectPath);
    //初始化Panel的地方
    //顶部状态栏的Panel
    private JPanel topPane = new JPanel(new BorderLayout());
    //左边功能条的panel.
    private JPanel functionPanel = new JPanel() {

        private String backgroundImageURL = PathLoad.getImageURL("side_BG.png");
        private TexturePaint paint = PathLoad.createTexturePaint(backgroundImageURL);

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setPaint(paint);
            g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
        }
    };
    
        //右边的panel 用图片做了填充.
    private JPanel rightPanel = new JPanel(){

        private String backgroundImageURL = PathLoad.getImageURL("mid_Color2.png");
        private TexturePaint paint = PathLoad.createTexturePaint(backgroundImageURL);

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setPaint(paint);
            g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
        }
    };

    private void init() {
        /**
         * 底层框架 JFrame @name frame;
         *
         * 设置 frame 大小、位置、是否可改变大小，是否见外框、是否应用默认布局、是否背景透明;
         *
         *
         */
        frame.setSize(300, 500);
        frame.setLocation(300, 100);
        frame.setResizable(false);                                              //不能改变大小
        frame.setUndecorated(true);
        frame.setLayout(null);                                                  //未使用布局
        frame.addMouseListener(moveWindowListener);                             //拖拽动作监听器
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                   //点击关闭窗体
        frame.setVisible(true);
        AWTUtilities.setWindowOpaque(frame, false);                             //frame完全透明！

        /**
         * 设置一个Container容器，这个容器继承了所有的Jframe的方法，往这个里面添加。 使用边界布局，创建窗口！
         */
        Container contentPane = frame.getContentPane();
        contentPane.setBounds(0, 0, 300, 500);
        contentPane.setLayout(new BorderLayout());

        /**
         * 设置关闭按钮的事件。
         */
        btnClose.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                System.exit(0);
            }
        });

        btnClose.setToolTipText("Close");


        /**
         * 设置顶部状态栏的样式，事件
         */

        topPane.setOpaque(false);                                               //顶部条要设置透明！

        topPane.addMouseListener(moveWindowListener);
        topPane.addMouseMotionListener(moveWindowListener);

        topPane.add(createDraggableLabel(this.topLeftPicPath), BorderLayout.WEST);
        topPane.add(createDraggableLabel(this.topMidPicPath), BorderLayout.CENTER);
        topPane.add(btnClose, BorderLayout.EAST);
        
        
        /**
         * 设置左边功能条
         */
        functionPanel.setBounds(0, 35, 30, 500);
        functionPanel.setPreferredSize(new Dimension(40, 423));

        /**
         * 设置右边可拖动栏
         */
        functionPanel.setPreferredSize(new Dimension(40, 423));

        contentPane.add(topPane, BorderLayout.NORTH);
        contentPane.add(functionPanel,BorderLayout.WEST);
        contentPane.add(rightPanel,BorderLayout.EAST);

        
        frame.setVisible(true);
        
    }
    /**
     * moveWindowListener 拖动以后移动一段距离。实现拖动效果
     */
    private MouseAdapter moveWindowListener = new MouseAdapter() {

        private Point lastPoint = null;

        @Override
        public void mousePressed(MouseEvent e) {
            lastPoint = e.getLocationOnScreen();
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            Point point = e.getLocationOnScreen();
            int offsetX = point.x - lastPoint.x;
            int offsetY = point.y - lastPoint.y;
            Rectangle bounds = frame.getBounds();
            bounds.x += offsetX;
            bounds.y += offsetY;
            frame.setBounds(bounds);
            lastPoint = point;
        }
    };

    /**
     * 创建一个有三种状态图象的按钮
     *
     * @param icon
     * @param roverIcon
     * @param pressedIcon
     * @return
     */
    private JButton createTransparentButton(ImageIcon icon, ImageIcon roverIcon, ImageIcon pressedIcon) {
        JButton button = new JButton();
        button.setBorder(null);
        button.setMargin(null);
        //  button.setOpaque(false);

        button.setContentAreaFilled(false);
        button.setIcon(icon);
        button.setRolloverEnabled(true);
        button.setRolloverIcon(roverIcon);
        button.setPressedIcon(pressedIcon);
        button.setContentAreaFilled(false);
        button.setFocusPainted(false);
        button.setRequestFocusEnabled(false);

        return button;
    }

    /**
     * 创建一个可以支持拖动的label框
     *
     * @param icon
     * @return
     */
    private JLabel createDraggableLabel(ImageIcon icon) {
        JLabel label = new JLabel(icon);
        label.addMouseListener(moveWindowListener);
        label.addMouseMotionListener(moveWindowListener);
        return label;
    }
}
